package com.ptmall.app.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import com.baidu.location.BDLocation;
import com.facebook.stetho.common.LogUtil;
import com.google.gson.Gson;
import com.ptmall.app.MainActivity;
import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.base.PermissionListener;
import com.ptmall.app.bean.HomePageArea;
import com.ptmall.app.bean.PageData;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.ui.adapter.HomePageAreaListAdapter;
import com.ptmall.app.utils.LocationUtil;
import com.ptmall.app.utils.SharedPreferencesHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/1/24.
 */

public class SeachXqActivity extends BaseMvpActivity {

    ListView listView;
    SharedPreferencesHelper mSharedPreferencesHelper ;

    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();
    HomePageAreaListAdapter mHomePageAreaListAdapter;
    EditText seach_edit;
    ImageView seach;
    double latitude, longitude;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSharedPreferencesHelper = new SharedPreferencesHelper(getContext(),"pt");
        setView(R.layout.activity_seachxiaoqu);
    }

    @Override
    public void initViews() {
        listView = (ListView) findViewById(R.id.listview);
        seach_edit = (EditText) findViewById(R.id.seach_edit);
        seach = (ImageView) findViewById(R.id.seach);
    }

    @Override
    public void initEvents() {
        mHomePageAreaListAdapter = new HomePageAreaListAdapter(getContext(),null);
        listView.setAdapter(mHomePageAreaListAdapter);
        initLocation();
        seach.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getHomePageAreaList();
            }
        });
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }


    private void initLocation() {
        requestRuntimePermisssions(new String[]{
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, new PermissionListener() {
            @Override
            public void onGranted() {
                LocationUtil.setCurrentAddress(new LocationUtil.currentAddress() {
                    @Override
                    public void getAddress(BDLocation location) {
                        if (location == null) {
                            showMsg("定位失败，请打开GPS定位功能");
                            LogUtil.d("xucc", "定位失败222");
                            return;
                        }
                        Log.d("xucc", new Gson().toJson(location));
//                        location_text.setText(location.getAddress().street);
                        longitude = location.getLongitude();
                        latitude = location.getLatitude();
                        getHomePageAreaList();
                    }
                }, getContext(), true);
            }

            @Override
            public void onDenied(List<String> deniedList) {
                showMsg("定位权限获取失败");
            }
        });
    }
    private void getHomePageAreaList(){
        Map<String,String> map = new HashMap<>();
        map.put("lat", latitude + "");
        map.put("lng", longitude + "");
        map.put("keyword",seach_edit.getText().toString());
        apiDataRepository.moreXq(map, new ApiNetResponse<PageData<HomePageArea>>(getContext()) {
            @Override
            public void onSuccess(PageData<HomePageArea> data) {
                mHomePageAreaListAdapter.pushData(data.getData());
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        mSharedPreferencesHelper.put("xiaoqu",data.getData().get(position).name);
                        startActivity(new Intent(getContext(), MainActivity.class));
                        finish();
                    }
                });
            }
        });
    }
}
